<?php

namespace App\Http\Controllers\Admin;


use App\Models\NewAdmin;
use App\Models\Order;
use App\Models\OrderDepict;
use App\Models\OrderLease;
use App\Models\OrderLeaseAudit;

use App\Models\OrderRemark;
use App\Models\ShopTrans;
use App\Services\AlipayService;
use App\Services\AuthService;
use App\Services\CommonService;

use App\Services\KuaiQianPayService;
use App\Services\WechatService;
use Illuminate\Http\Request;

class OrderDepositController extends BaseController
{

    /**
     * 提前支付审核
     */
    public function orderDepositList(Request $request){
        $request_data = $request->all();
        $list = (new OrderDepict())->checkList($request_data);

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$list);
    }
    public function orderRefundDeposit(Request $request){

        $orderDepict=OrderDepict::query()->where('id',$request['id'])->first();

        if(!is_numeric($orderDepict['order_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择订单');
        }
        if(empty($orderDepict['order_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单不存在');
        }

        $userToken = $request->header("Authorization");
        $user = NewAdmin::where(['token'=>$userToken,'type'=>AuthService::MODEL_ADMIN])->first();
        $user = obToArray($user);

        if(md5($user['mobile'].$request['password']) != $user['password']){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'密码输入错误错误');
        }

        if($orderDepict['refund_deposit_status']!=1||empty($orderDepict['alipay_trade_no'])||empty($orderDepict['deposit_amount'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单押金未支付');
        }
        if($orderDepict['refund_deposit_status']==2){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单押金已退款');
        }

        $order=Order::query()->where('id',$orderDepict['order_id'])->select('id','status','trade_no','mini_id')->first();
        if($order['status']==3){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'当前订单状态不可退款');
        }
        if($order['status']>4&&$order['status']!=60&&$order['status']!=16){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'当前订单状态不可退款');
        }


        if($orderDepict['pay_type']==1){
            $refund_data['amount'] =  $orderDepict['deposit_amount'];
            $refund_data['outTradeNo'] = '';
            $refund_data['tradeNo'] =  $orderDepict['alipay_trade_no'];
            $refund_data['out_request_no'] = $orderDepict['alipay_trade_no'].rand(100,999);
            $refund_res = AlipayService::alipayRefund($order['mini_id'],$refund_data);
            if(!$refund_res['status']){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,$refund_res['data']['data']['sub_msg']);
            }
        }else if($orderDepict['pay_type']==2){
            $deposit_amount=bcmul($orderDepict['deposit_amount'],100,0);
            $pay_amount=bcmul($orderDepict['pay_amount'],100,0);
            $refund_res = WechatService::refund($orderDepict['out_trade_no'],$orderDepict['alipay_trade_no'],$deposit_amount,$pay_amount);
            if($refund_res['result_code']!='SUCCESS'){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'微信支付'.$refund_res['err_code_des'],'data'=>[]];
            }
        }else if($orderDepict['pay_type']==OrderLease::PAY_TYPE_YIFUBAO){

                $out_request_no = $order['trade_no'].time();

                $param['refundOrderNo']=$out_request_no;
                $param['origMerchantOrderNo']=$orderDepict['alipay_trade_no'];
                $param['origOrderTime']=$orderDepict['pay_time'];
                $param['refundAmount']=bcmul($orderDepict['deposit_amount'],100);
                $param['operator']='退押金';
                $param['refundReason']='退押金';

                $KuaiQianPayService=new KuaiQianPayService();
                $refund_res=$KuaiQianPayService->refund($param);
                if(!$refund_res){
                    return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'银行卡支付'.$refund_res['responseMsg'],'data'=>[]];
                }

        }


         $res=OrderDepict::query()->where('id',$request['id'])->update(['refund_deposit_status'=>2]);
            $add = [
                'uid'=>$this->users['id'],
                'username'=>$this->users['username'],
                'create_time'=>time(),
                'remark'=>$this->users['username'].'订单押金退款成功',
                'order_id'=>$order['id']
            ];
            OrderRemark::insert($add);
            if($res){
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'退款成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,'退款失败');
            }
    }

    //退碎屏险
    public function orderRefundScreen(Request $request){

        $orderDepict=OrderDepict::query()->where('id',$request['id'])
            ->select('alipay_trade_no','status','order_id','pay_amount','pay_type','screen_amount','out_trade_no','refund_screen_status')->first();

        if(!is_numeric($orderDepict['order_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'请选择订单');
        }
        if(empty($orderDepict['order_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'订单不存在');
        }

        $userToken = $request->header("Authorization");
        $user = NewAdmin::where(['token'=>$userToken,'type'=>AuthService::MODEL_ADMIN])->first();
        $user = obToArray($user);

        if(md5($user['mobile'].$request['password']) != $user['password']){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'密码输入错误错误');
        }

        if($orderDepict['refund_screen_status']!=1||empty($orderDepict['alipay_trade_no'])||empty($orderDepict['screen_amount'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'碎屏险未支付');
        }
        if($orderDepict['refund_screen_status']==2){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'碎屏险已退款');
        }

        $order=Order::query()->where('id',$orderDepict['order_id'])->select('id','status','mini_id')->first();
        if($order['status']==3){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'当前订单状态不可退款');
        }
        if($order['status']>4&&$order['status']!=60&&$order['status']!=16){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'当前订单状态不可退款');
        }


        if($orderDepict['pay_type']==1){
            $refund_data['amount'] =  $orderDepict['screen_amount'];
            $refund_data['outTradeNo'] = '';
            $refund_data['tradeNo'] =  $orderDepict['alipay_trade_no'];
            $refund_data['out_request_no'] = $orderDepict['alipay_trade_no'].rand(100,999);
            $refund_res = AlipayService::alipayRefund($order['mini_id'],$refund_data);
            if(!$refund_res['status']){
                return ReturnMsg(CommonService::HTTP_CODE_FAIL,$refund_res['data']['data']['sub_msg']);
            }
        }else if($orderDepict['pay_type']==2){
            $screen_amount=bcmul($orderDepict['screen_amount'],100,0);
            $pay_amount=bcmul($orderDepict['pay_amount'],100,0);

            $refund_res = WechatService::refund($orderDepict['out_trade_no'],$orderDepict['alipay_trade_no'],$screen_amount,$pay_amount);
            if($refund_res['result_code']!='SUCCESS'){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'微信支付'.$refund_res['err_code_des'],'data'=>[]];
            }
        }else if($orderDepict['pay_type']==OrderLease::PAY_TYPE_YIFUBAO){

            $out_request_no = $order['trade_no'].time();

            $param['refundOrderNo']=$out_request_no;
            $param['origMerchantOrderNo']=$orderDepict['alipay_trade_no'];
            $param['origOrderTime']=$orderDepict['pay_time'];
            $param['refundAmount']=bcmul($orderDepict['screen_amount'],100);
            $param['operator']='退押金';
            $param['refundReason']='退押金';

            $KuaiQianPayService=new KuaiQianPayService();
            $refund_res=$KuaiQianPayService->refund($param);
            if(!$refund_res){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'银行卡支付'.$refund_res['responseMsg'],'data'=>[]];
            }

        }

        $res=OrderDepict::query()->where('id',$request['id'])->update(['refund_screen_status'=>2]);
        $add = [
            'uid'=>$this->users['id'],
            'username'=>$this->users['username'],
            'create_time'=>time(),
            'remark'=>$this->users['username'].'订单碎屏险退款成功',
            'order_id'=>$order['id']
        ];
        OrderRemark::insert($add);
        if($res){
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'退款成功');
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'退款失败');
        }


    }


}
